package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IFeedbackService;
import com.mdd.admin.validate.FeedbackCreateValidate;
import com.mdd.admin.validate.FeedbackUpdateValidate;
import com.mdd.admin.validate.FeedbackSearchValidate;
import com.mdd.admin.vo.FeedbackListedVo;
import com.mdd.admin.vo.FeedbackDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Feedback;
import com.mdd.common.mapper.FeedbackMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 意见反馈实现类
 * @author LikeAdmin
 */
@Service
public class FeedbackServiceImpl implements IFeedbackService {
        
    @Resource
    FeedbackMapper feedbackMapper;

    /**
     * 意见反馈列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FeedbackListedVo>
     */
    @Override
    public PageResult<FeedbackListedVo> list(PageValidate pageValidate, FeedbackSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        feedbackMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:content:str",
            "like:name:str",
            "like:phone:str",
            "=:company:str",
            "=:status:int",
            "=:remark:str",
        });

        IPage<Feedback> iPage = feedbackMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<FeedbackListedVo> list = new LinkedList<>();
        for(Feedback item : iPage.getRecords()) {
            FeedbackListedVo vo = new FeedbackListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 意见反馈详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Feedback
     */
    @Override
    public FeedbackDetailVo detail(Integer id) {
        Feedback model = feedbackMapper.selectOne(
                new QueryWrapper<Feedback>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        FeedbackDetailVo vo = new FeedbackDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 意见反馈新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(FeedbackCreateValidate createValidate) {
        Feedback model = new Feedback();
        model.setUserId(createValidate.getUserId());
        model.setContent(createValidate.getContent());
        model.setName(createValidate.getName());
        model.setPhone(createValidate.getPhone());
        model.setCompany(createValidate.getCompany());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setStatus(createValidate.getStatus());
        model.setRemark(createValidate.getRemark());
        feedbackMapper.insert(model);
    }

    /**
     * 意见反馈编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(FeedbackUpdateValidate updateValidate) {
        Feedback model = feedbackMapper.selectOne(
                new QueryWrapper<Feedback>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setContent(updateValidate.getContent());
        model.setName(updateValidate.getName());
        model.setPhone(updateValidate.getPhone());
        model.setCompany(updateValidate.getCompany());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setStatus(updateValidate.getStatus());
        model.setRemark(updateValidate.getRemark());
        feedbackMapper.updateById(model);
    }

    /**
     * 意见反馈删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Feedback model = feedbackMapper.selectOne(
                new QueryWrapper<Feedback>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        feedbackMapper.delete(new QueryWrapper<Feedback>().eq("id", id));
    }

}
